package com.magic.smartcity.model;

import android.util.Log;

import com.magic.smartcity.beans.FeedbackBean;
import com.magic.smartcity.beans.pojo.FeedbackBody;
import com.magic.smartcity.beans.pojo.LoginPojo;
import com.magic.smartcity.beans.RegisterBody;
import com.magic.smartcity.beans.UserBean;
import com.magic.smartcity.beans.pojo.ResultList;
import com.magic.smartcity.beans.pojo.UpdatePasswordPojo;
import com.magic.smartcity.interfaces.IUserModel;
import com.magic.smartcity.model.network.UserService;
import com.magic.smartcity.presenters.SystemServicePresenter;
import com.magic.smartcity.presenters.UserPresenter;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;

/**
 * @description:
 * @create: Create by Magic on 2022/2/8.
 */
public class UserModel implements IUserModel {
    private static final String TAG = UserModel.class.getSimpleName();

    @Inject
    Retrofit mRetrofit;

    @Inject
    UserModel() {
    }

    @Override
    public void getNowUser() {
        UserService userService = mRetrofit.create(UserService.class);
        userService.getUser()
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> {
                    throwable.printStackTrace();
                })
                .subscribe(userBean -> {
                    if (userBean.getCode() == 200) {
                        UserPresenter.UserEventBusMessage message = new UserPresenter.UserEventBusMessage();
                        message.setUser(userBean.getUser());
                        EventBus.getDefault().post(message);
                    }
                });
    }

    @Override
    public void loginUser(LoginPojo user) {
        UserService userService = mRetrofit.create(UserService.class);
        userService.loginUser(user)
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> {
                    throwable.printStackTrace();
                })
                .subscribe(tokenBean -> {
                    Log.d(TAG, "============登录成功============");
                    UserPresenter.UserEventBusMessage message = new UserPresenter.UserEventBusMessage();
                    message.setTokenBean(tokenBean);
                    EventBus.getDefault().post(message);
                });
    }

    @Override
    public void register(RegisterBody registerBody) {
        UserService userService = mRetrofit.create(UserService.class);
        userService.register(registerBody)
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> {
                    throwable.printStackTrace();
                })
                .subscribe(registerBean -> {
                    UserPresenter.UserEventBusMessage message = new UserPresenter.UserEventBusMessage();
                    message.setRegisterBean(registerBean);
                    EventBus.getDefault().post(message);
                });
    }

    @Override
    public void updateUser(UserBean.UserBeanDo user) {
        UserService userService = mRetrofit.create(UserService.class);
        userService.updateUser(user).observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> throwable.printStackTrace())
                .subscribe(registerBean -> {
                    UserPresenter.UserEventBusMessage message = new UserPresenter.UserEventBusMessage();
                    message.setUpdateUser(registerBean);
                    EventBus.getDefault().post(message);
                });
    }

    @Override
    public void updatePassword(UpdatePasswordPojo updatePasswordPojo) {
        UserService userService = mRetrofit.create(UserService.class);
        userService.updatePassword(updatePasswordPojo)
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> throwable.printStackTrace())
                .subscribe(msgCodeBean -> {
                    UserPresenter.UserEventBusMessage message = new UserPresenter.UserEventBusMessage();
                    message.setPasswordUpdateResult(msgCodeBean);
                    EventBus.getDefault().post(message);
                });
    }

    @Override
    public void feedbackSubmit(FeedbackBody bean) {
        UserService userService = mRetrofit.create(UserService.class);
        userService.submitFeedback(bean)
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> throwable.printStackTrace())
                .subscribe(resultData -> {
                    UserPresenter.UserEventBusMessage message = new UserPresenter.UserEventBusMessage();
                    message.setSubmitFeedbackResult(resultData);
                    EventBus.getDefault().post(message);
                });
    }

    @Override
    public void getFeedbackList() {
        UserService userService = mRetrofit.create(UserService.class);
        Log.d(TAG, "request:feedbackList");
        userService.getFeedbackList()
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> throwable.printStackTrace())
                .subscribe(feedbackBeanResultList -> {
                    UserPresenter.UserEventBusMessage message = new UserPresenter.UserEventBusMessage();
                    message.setFeedbackBeanResultList(feedbackBeanResultList);
                    EventBus.getDefault().post(message);

                });
    }

    @Override
    public void getFeedbackById(int id) {
        UserService userService = mRetrofit.create(UserService.class);
        userService.getFeedbackById(id)
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> throwable.printStackTrace())
                .subscribe(resultData -> {
                    UserPresenter.UserEventBusMessage message = new UserPresenter.UserEventBusMessage();
                    message.setResultData(resultData);
                    EventBus.getDefault().post(message);
                });

    }

    @Override
    public void getBalanceList() {
        UserService userService = mRetrofit.create(UserService.class);
        userService.getBalanceList()
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> throwable.printStackTrace())
                .subscribe(balanceBeanResultList -> {
                    UserPresenter.UserEventBusMessage message = new UserPresenter.UserEventBusMessage();
                    message.setBalanceResultList(balanceBeanResultList);
                    EventBus.getDefault().post(message);
                });
    }

    @Override
    public void balanceRecharge(int money) {
        UserService userService = mRetrofit.create(UserService.class);
        userService.balanceRecharge(money)
                .observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .doOnError(throwable -> throwable.printStackTrace())
                .subscribe(resultData -> {
                    UserPresenter.UserEventBusMessage message = new UserPresenter.UserEventBusMessage();
                    message.setRechargeBalanceData(resultData);
                    EventBus.getDefault().post(message);
                });
    }

}
